package com.akilesa.dictcreator;

import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: akilesa
 * Date: 19.01.2010
 * Time: 11:20:36
 * To change this template use File | Settings | File Templates.
 */

/**
 * NAME:
 * <p/>
 * DESCRIPTION:
 * -f - exclude words with frequency 1
 */
public class DictCreatorApp {

    private static final String EXCLUDE_FREQUENCY_ONE_OPTION = "-f";
    private static final String PRINT_TO_CONSOLE_OPTION = "-c";
    private static Map<String, Boolean> parameters = new HashMap<String, Boolean>();

    public static void main(String[] args) {

        prepareParameters(args);
        String filename = args[args.length - 2];
        EnglishWordTokenizer tokenizer = new EnglishWordTokenizer();
        try {
            EnglishWordTokenizerResult result = tokenizer.divideOnWords(readFromFile(filename));
            if (parameters.get(EXCLUDE_FREQUENCY_ONE_OPTION)) {
                result.excludeFrequencyOne();
            }
            if (parameters.get(PRINT_TO_CONSOLE_OPTION)) {
                result.printToConsole();
            }
            AnkiCardsCreator creator=new AnkiCardsCreator();
            List<AnkiCard> cards=creator.create(result);
            AnkiDbManagerImpl dbManager=new AnkiDbManagerImpl();
            String deckName=args[args.length-1];
            dbManager.createAnkiDeck(deckName);
            dbManager.createCards(cards,deckName);
            Statistic.getInstance().print();
        } catch (IOException e) {
            System.out.println("Can't open file " + filename);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }        

    protected static String readFromFile(String filename) throws IOException {
        FileReader reader = null;
        try {
            reader = new FileReader(filename);
            String result = "";
            char[] buf = new char[1000];
            while (reader.ready()) {
                int count = reader.read(buf);
                result += new String(buf, 0, count);
            }
            return result;
        } finally {
            reader.close();
        }
    }

    protected static void prepareParameters(String[] args) {
        if (args.length > 2) {
            for (int i = 0; i < args.length - 2; i++) {
                if (args[i].equals(EXCLUDE_FREQUENCY_ONE_OPTION)) {
                    parameters.put(EXCLUDE_FREQUENCY_ONE_OPTION, true);
                } else {
                    parameters.put(EXCLUDE_FREQUENCY_ONE_OPTION, false);
                }
                if (args[i].equals(PRINT_TO_CONSOLE_OPTION)) {
                    parameters.put(PRINT_TO_CONSOLE_OPTION, true);
                } else {
                    parameters.put(PRINT_TO_CONSOLE_OPTION, false);
                }
            }
        } else {
            System.out.println("Parameters File name and Deck name must be defined");
            System.exit(1);
        }
    }
}
